import { MockMethod } from "vite-plugin-mock";
import { resultError, resultSuccess, getRequestToken, requestParams } from "../_util";

export function createFakeUserList() {
  return [
    {
      userCode: "2001111",
      userName: "Suomm",
      gender: 2,
      avatar: "https://q1.qlogo.cn/g?b=qq&nk=190848757&s=640",
      introduction: "manager",
      college: {
        collegeId: 1,
        collegeName: "化学学院",
        leader: "王帅",
        phone: "10086",
      },
    },
    {
      userId: "2",
      username: "test",
      password: "123456",
      realName: "test user",
      avatar: "https://q1.qlogo.cn/g?b=qq&nk=339449197&s=640",
      desc: "tester",
      token: "fakeToken2",
      homePath: "/dashboard/workbench",
      roles: [
        {
          roleName: "Tester",
          value: "test",
        },
      ],
    },
  ];
}

const fakeCodeList = [["ROLE_INSTRUCTOR", "ROLE_CAPTAIN"]];
export default [
  {
    url: "/dev-api/login",
    timeout: 200,
    method: "get",
    rawResponse: (_req, res) => {
      res.statusCode = 401;
      res.write("https://localhost:3100/login?token=faketoken");
      res.end();
    },
  },
  {
    url: "/dev-api/getInfo",
    method: "get",
    response: (request: requestParams) => {
      const token = getRequestToken(request);
      if (!token) return resultError("Invalid token");
      const checkUser = createFakeUserList().at(0);
      if (!checkUser) {
        return resultError("The corresponding user information was not obtained!");
      }
      return resultSuccess(checkUser);
    },
  },
  {
    url: "/dev-api/user/getProfile/:id",
    method: "get",
    response: () => {
      return resultSuccess(createFakeUserList().at(0));
    },
  },
  {
    url: "/dev-api/getRoles",
    timeout: 200,
    method: "get",
    response: (request: requestParams) => {
      const token = getRequestToken(request);
      if (!token) return resultError("Invalid token");

      return resultSuccess(fakeCodeList[0]);
    },
  },
  {
    url: "/dev-api/logout",
    timeout: 200,
    method: "get",
    response: (request: requestParams) => {
      const token = getRequestToken(request);
      if (!token) return resultError("Invalid token");
      return resultSuccess(undefined, { message: "Token has been destroyed" });
    },
  },
  {
    url: "/dev-api/testRetry",
    statusCode: 405,
    method: "get",
    response: () => {
      return resultError("Error!");
    },
  },
] as MockMethod[];
